<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
		/* Standard Libraries */
		$this->load->database();
		$this->load->helper('url');
		/* ------------------ */	
		$this->load->model('model_messages');
		$this->data['message'] = $this->model_messages->get_message();
		
		$this->load->model('model_footer');
		$this->data['footer'] = $this->model_footer->get_footer();	
		
		$this->load->model('model_shops');
		$this->data['promo_shops'] = $this->model_shops->shops_promo();
		
		$this->data['seo_title'] = '';
		$this->data['seo_description'] = '';
		$this->data['seo_keywords'] = '';
		
		$this->load->library('grocery_CRUD');	
	}
	
	function _example_output($output = null)
	{	
		$this->load->view('general/header', $this->data);
		$this->load->view('member_admins',$output);
		$this->load->view('general/footer', $this->data);
	}
	
	function index()
	{
		$this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
	}	
	
	function users()
	{
	
		if($this->session->userdata('is_logged_in')) {
			$output = $this->grocery_crud->render();

			$this->_example_output($output);
		} else {
			redirect('main/index');
		}
	}
	
	function footer_groups()
	{
		if($this->session->userdata('is_logged_in')) {
			$output = $this->grocery_crud->render();

			$this->_example_output($output);
		} else {
			redirect('main/index');
		}
	}
	
	function footer_links()
	{
		if($this->session->userdata('is_logged_in')) {
			$crud = new grocery_CRUD();

			$crud->set_theme('datatables');
			$crud->set_table('footer_links');
			$crud->set_relation('group_id','footer_groups','name');
			$crud->display_as('group_id','Categorie');
			
			$output = $crud->render();

			$this->_example_output($output);
		} else {
			redirect('main/index');
		}
	}
	
	
	function categories()
	{
	
		if($this->session->userdata('is_logged_in')) {
			$output = $this->grocery_crud->render();

			$this->_example_output($output);
		} else {
			redirect('main/index');
		}
	}
	
	function pages()
	{
	
		if($this->session->userdata('is_logged_in')) {
			$output = $this->grocery_crud->render();

			$this->_example_output($output);
		} else {
			redirect('main/index');
		}
	}
	
	function shops()
	{
		$crud = new grocery_CRUD();

		$crud->set_theme('datatables');
		$crud->set_table('shops');
		$crud->set_relation('cat_id','categories','name');
		$crud->display_as('cat_id','Categorie');
		
		$crud->set_field_upload('image','assets/uploads/');
		
		$output = $crud->render();

		$this->_example_output($output);
	}
	
	function customers_management()
	{
			$crud = new grocery_CRUD();

			$crud->set_table('customers');
			$crud->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit');
			$crud->display_as('salesRepEmployeeNumber','from Employeer')
				 ->display_as('customerName','Name')
				 ->display_as('contactLastName','Last Name');
			$crud->set_subject('Customer');
			$crud->set_relation('salesRepEmployeeNumber','employees','{lastName} {firstName}');
			
			$output = $crud->render();
			
			$this->_example_output($output);
	}	
	
	function orders_management()
	{
			$crud = new grocery_CRUD();

			$crud->set_relation('customerNumber','customers','{contactLastName} {contactFirstName}');
			$crud->display_as('customerNumber','Customer');
			$crud->set_table('orders');
			$crud->set_subject('Order');
			$crud->unset_add();
			$crud->unset_delete();
			
			$output = $crud->render();
			
			$this->_example_output($output);
	}
	
	function products_management()
	{
			$crud = new grocery_CRUD();

			$crud->set_table('products');
			$crud->set_subject('Product');
			$crud->unset_columns('productDescription');
			$crud->callback_column('buyPrice',array($this,'valueToEuro'));
			
			$output = $crud->render();
			
			$this->_example_output($output);
	}	
	
	function valueToEuro($value, $row)
	{
		return $value.' &euro;';
	}
	
	function film_management()
	{
		$crud = new grocery_CRUD();
		
		$crud->set_table('film');
		$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');
		$crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name');
		$crud->unset_columns('special_features','description');
		
		$crud->fields('title', 'description', 'actors' ,  'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
		
		$output = $crud->render();
		
		$this->_example_output($output);
	}
	
}